package com.atguigu.tingshu.album.service.impl;

import cn.hutool.json.JSONObject;
import com.atguigu.tingshu.album.mapper.*;
import com.atguigu.tingshu.album.service.BaseCategoryService;
import com.atguigu.tingshu.model.album.*;
import com.atguigu.tingshu.query.album.AlbumInfoQuery;
import com.atguigu.tingshu.vo.album.AlbumListVo;
import com.atguigu.tingshu.vo.album.CategoryNode;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
@SuppressWarnings({"all"})
public class BaseCategoryServiceImpl extends ServiceImpl<BaseCategory1Mapper, BaseCategory1> implements BaseCategoryService {

    @Autowired
    private BaseCategory1Mapper baseCategory1Mapper;

    @Autowired
    private BaseCategory2Mapper baseCategory2Mapper;

    @Autowired
    private BaseCategory3Mapper baseCategory3Mapper;

    @Autowired
    private BaseCategoryViewMapper baseCategoryViewMapper;
    @Autowired
    BaseAttributeMapper baseAttributeMapper;

    @Override
    public List<JSONObject> getBaseCategoryList() {
        List<JSONObject> list = new ArrayList<>();//一级分类

        List<BaseCategoryView> baseCategoryViews = baseCategoryViewMapper.selectList(null);//查询所有分类数据
        if (!CollectionUtils.isEmpty(baseCategoryViews)) {
            Map<Long, List<BaseCategoryView>> collect = baseCategoryViews.stream().collect(Collectors.groupingBy(BaseCategoryView::getCategory1Id));
            for (Map.Entry<Long, List<BaseCategoryView>> listEntry : collect.entrySet()) {
                //创建一级分类对象
                JSONObject jsonObject1 = new JSONObject();

                List<BaseCategoryView> catagory2List = listEntry.getValue();
                String category1Name = catagory2List.get(0).getCategory1Name();
                jsonObject1.put("categoryName", category1Name);
                jsonObject1.put("categoryId", listEntry.getKey());
                //封装二级分类
                List<JSONObject> array2 = new ArrayList<>();
                Map<Long, List<BaseCategoryView>> collect1 = catagory2List.stream().collect(Collectors.groupingBy(BaseCategoryView::getCategory2Id));
                for (Map.Entry<Long, List<BaseCategoryView>> c2tEntry : collect1.entrySet()) {
                    Long c2Id = c2tEntry.getKey();
                    List<BaseCategoryView> catagory3List = c2tEntry.getValue();
                    String category2Name = catagory3List.get(0).getCategory2Name();
                    //创建二级分类对象
                    JSONObject jsonObject2 = new JSONObject();
                    jsonObject2.put("categoryName", category2Name);
                    jsonObject2.put("categoryId", c2Id);
                    //封装三级分类
                    List<JSONObject> array3 = new ArrayList<>();
                    for (BaseCategoryView catagory3 : catagory3List) {
                        //创建三级分类对象
                        JSONObject jsonObject3 = new JSONObject();
                        jsonObject3.put("categoryName", catagory3.getCategory3Name());
                        jsonObject3.put("categoryId", catagory3.getCategory3Id());
                        array3.add(jsonObject3);
                    }

                    jsonObject2.put("categoryChild", array3);
                    array2.add(jsonObject2);


                }
                jsonObject1.put("categoryChild", array2);
                list.add(jsonObject1);

            }

        }

        return list;
    }

    @Override
    public List<BaseAttribute> findAttribute(Long category1Id) {
        List<BaseAttribute> list = baseAttributeMapper.findAttribute(category1Id);
        return list;
    }

    @Override
    public BaseCategoryView getCategoryView(Long category3Id) {
        return baseCategoryViewMapper.selectById(category3Id);

    }

    @Override
    public List<BaseCategory3> findTopBaseCategory3(Long category1Id) {
        // 根据分类1Id查询分类2Id
        LambdaQueryWrapper<BaseCategory2> bc2Wrapper = new LambdaQueryWrapper<>();
        bc2Wrapper.eq(BaseCategory2::getCategory1Id, category1Id);
        List<BaseCategory2> category2List = baseCategory2Mapper.selectList(bc2Wrapper);
        List<Long> category2IdList = category2List.stream().map(category2 -> {
            return category2.getId();
        }).collect(Collectors.toList());
        LambdaQueryWrapper<BaseCategory3> bc3Wrapper = new LambdaQueryWrapper<>();
        List<BaseCategory3> baseCategory3s = new ArrayList<>();
        category2IdList.forEach(item -> {
            bc3Wrapper.eq(BaseCategory3::getCategory2Id, item)
                    .eq(BaseCategory3::getIsTop, 1)
                    .orderByAsc(BaseCategory3::getOrderNum)
                    .last("limit 7");
            List<BaseCategory3> category3List = baseCategory3Mapper.selectList(bc3Wrapper);
            baseCategory3s.addAll(category3List);
        });


        return baseCategory3s;
    }

    @Override
    public JSONObject getBaseCategoryListByCategory1Id(Long category1Id) {
        JSONObject jsonObject = new JSONObject();
        List<BaseCategoryView> baseCategoryViews = baseCategoryViewMapper.selectList
                (new LambdaQueryWrapper<BaseCategoryView>().eq(BaseCategoryView::getCategory1Id, category1Id));
        if (baseCategoryViews.size() > 0) {
            BaseCategoryView baseCategoryView = baseCategoryViews.get(0);
            jsonObject.put("categoryId", baseCategoryView.getCategory1Id());
            jsonObject.put("categoryName", baseCategoryView.getCategory1Name());
            Map<Long, List<BaseCategoryView>> collect = baseCategoryViews.stream().collect(Collectors.groupingBy(BaseCategoryView::getCategory2Id));
            ArrayList<JSONObject> categoryList2 = new ArrayList<>();
            for (Map.Entry<Long, List<BaseCategoryView>> listEntry : collect.entrySet()) {
                //创建二级级分类对象
                JSONObject object2 = new JSONObject();
                List<BaseCategoryView> catagory3List = listEntry.getValue();
                String category2Name = catagory3List.get(0).getCategory2Name();
                object2.put("categoryName", category2Name);
                object2.put("categoryId", listEntry.getKey());
                List<JSONObject> collect3 = catagory3List.stream().map(baseCategoryView1 -> {
                    JSONObject jsonObject3 = new JSONObject();
                    jsonObject3.put("categoryName", baseCategoryView1.getCategory3Name());
                    jsonObject3.put("categoryId", baseCategoryView1.getCategory3Id());
                    return jsonObject3;
                }).collect(Collectors.toList());

                object2.put("categoryChild", collect3);
                categoryList2.add(object2);


            }
            jsonObject.put("categoryChild", categoryList2);//一级菜单的children


        }
        return jsonObject;

    }
      /*  //根据category1Id查出当前一级分类的信息
        BaseCategory1 baseCategory1 = baseCategory1Mapper.selectById(category1Id);
        CategoryNode categoryNode1 = new CategoryNode();
        categoryNode1.setCategoryId(baseCategory1.getId());
        categoryNode1.setCategoryName(baseCategory1.getName());
        //获取二级分类信息
        List<BaseCategory2> category2List = baseCategory2Mapper
                .selectList(new QueryWrapper<BaseCategory2>()
                        .eq("category1_id", category1Id));
        ArrayList<CategoryNode> categoryNodeList1 = new ArrayList<>();

        category2List.forEach(
                category2 -> {
                    if (category2.getCategory1Id().equals(category1Id)) {
                        CategoryNode categoryNode2 = new CategoryNode();
                        categoryNode2.setCategoryId(category2.getId());
                        categoryNode2.setCategoryName(category2.getName());
                        //获取category3信息
                        List<BaseCategory3> category3List = baseCategory3Mapper
                                .selectList(new QueryWrapper<BaseCategory3>()
                                        .eq("category2_id", category2.getId()));
                        ArrayList<CategoryNode> categoryNodeList2 = new ArrayList<>();
                        category3List.forEach(
                                category3 -> {

                                    if (category3.getCategory2Id().equals(category2.getId())) {
                                        CategoryNode categoryNode3 = new CategoryNode();
                                        categoryNode3.setCategoryId(category3.getId());
                                        categoryNode3.setCategoryName(category3.getName());
                                        categoryNodeList2.add(categoryNode3);
                                        categoryNode2.setCategoryChild(categoryNodeList2);
                                    }

                                });
                        categoryNodeList1.add(categoryNode2);
                    }

                    categoryNode1.setCategoryChild(categoryNodeList1);
                });

        return categoryNode1;
    }*/


}
